// 引入路由 和 静态路由
import router, {DynamicRoutes,constantRoutes} from "@/router";
import {getRoutes} from "../../http/api/login";


const auth = {
    state: {
        // 菜单路由
        menuList: [],
        //完整路由
        routes:[],
    },
    mutations: {
        SET_ROUTES(state,routes){
            state.routes = routes
        },
        SET_MENULIST(state,menuList){
            state.menuList = menuList
        }
    },
    actions:{
        generateRoutes({commit}){
            return new Promise((resolve, reject) => {
                getRoutes().then(res=>{
                    // 静态路由 +  动态路由  合并  完整路由
                    const array = constantRoutes.concat(res)
                    commit("SET_MENULIST",array)
                    commit("SET_ROUTES",array)
                    router.options.routes = array
                    router.addRoutes([...array])
                    resolve()
                })
            })
        }
    }
};
export default auth;
